package com.sfdk.graphics.engine.animation
{
    import com.sfdk.data.RectangleID;
    import com.sfdk.graphics.engine.api.IAnimatedBitmap;
    import com.sfdk.io.GenericIO;
    import com.sfdk.pools.BitmapDataPool;

    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;

    public class AnimatedBitmap extends GenericIO implements IAnimatedBitmap
    {
        protected var _animationData:Dictionary;
        protected var _animationTile:Vector.<BitmapData>;
        protected var _animationLength:int;

        public function AnimatedBitmap(animatedObjectPath:String = null) {
            _animationTile = new Vector.<BitmapData>();
            super(animatedObjectPath, GenericIO.DATA_FILE);
        }

        protected var _isLoaded:Boolean = false;

        public function get isLoaded():Boolean {
            return _isLoaded;
        }

        override protected function init():void {
            super.init();

            var byteArray:ByteArray = getByteData();
            byteArray.uncompress();
            byteArray.position = 0;

            var imageWidth:int = byteArray.readUnsignedInt();
            var imageHeight:int = byteArray.readUnsignedInt();
            var imageSize:int = byteArray.readUnsignedInt();

            var imageByteArray:ByteArray = new ByteArray();
            byteArray.readBytes(imageByteArray, 0, imageSize);

            var imageBitmapData:BitmapData = new BitmapData(imageWidth, imageHeight);
            imageBitmapData.setPixels(new Rectangle(0, 0, imageWidth, imageHeight), imageByteArray);

            var splicingData:Array = byteArray.readObject() as Array;
            var animationData:Array = byteArray.readObject() as Array;

            // Splicing handling
            var splicingObject:RectangleID;
            var splicingBitmap:BitmapData;
            var splicingIndex:int;
            var maxSlicingId:int;
            splicingData.sortOn("id", Array.NUMERIC);
            for each (splicingObject in splicingData) {
                splicingBitmap = new BitmapData(splicingObject.width, splicingObject.height);
                splicingBitmap.copyPixels(imageBitmapData, splicingObject, new Point());
                _animationTile[splicingObject.id] = splicingBitmap;
                maxSlicingId = Math.max(maxSlicingId, splicingObject.id);
                splicingIndex++;
            }
            maxSlicingId++;
            _animationTile[maxSlicingId] = BitmapDataPool.instance.getFromPool("resources/images/death1.png");
            _animationTile[maxSlicingId + 1] = BitmapDataPool.instance.getFromPool("resources/images/death2.png");
            _animationTile[maxSlicingId + 2] = BitmapDataPool.instance.getFromPool("resources/images/death3.png");

            _animationLength = _animationTile.length;

            var actionChooserDP:Vector.<String> = new <String>["Statique", "Marche"];
            var faceChooserDP:Vector.<String> = new <String>["Face", "Dos", "Gauche", "Droite"];
            var currFace:String;
            var currAction:String;
            var faceIndex:int;
            var actionIndex:int;
            var subActionArray:Array;
            var subFaceArray:Array;

            _animationData = new Dictionary();
            for each(currFace in faceChooserDP) {
                subFaceArray = animationData[faceIndex];
                actionIndex = 0;
                for each(currAction in actionChooserDP) {
                    subActionArray = subFaceArray[actionIndex];
                    _animationData[currAction + "_" + currFace] = subActionArray;
                    actionIndex++;
                }
                faceIndex++;
            }

            _animationData["Marche_death1"] = [
                {duration:0,id:maxSlicingId,xOffset:0, yOffset:0}
            ]
            _animationData["Marche_death2"] = [
                {duration:0,id:maxSlicingId + 1,xOffset:0, yOffset:0}
            ]
            _animationData["Marche_death3"] = [
                {duration:0,id:maxSlicingId + 2,xOffset:0, yOffset:0}
            ]

            dispatchEvent(new Event("animationLoaded"));
            _isLoaded = true;
        }

        public function getAnimationData(face:String, action:String):Array {
            return _animationData[action + "_" + face];
        }

        public function get maxIndex():int {
            return _animationTile.length;
        }

        public function getView(tileID:int):BitmapData {
            if (tileID < _animationLength)
                return _animationTile[tileID];
            else
                return null;
        }

        public function get animationTile():Vector.<BitmapData> {
            return _animationTile;
        }

        public function set animationTile(value:Vector.<BitmapData>):void {
            _animationTile = value;
        }

        public function get animationData():Dictionary {
            return _animationData;
        }

        public function set animationData(value:Dictionary):void {
            _animationData = value;
        }

        public function get animationLength():int {
            return _animationLength;
        }

        public function set animationLength(value:int):void {
            _animationLength = value;
        }


    }
}